package com.feihong.txgw.app;

import android.annotation.TargetApi;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.multidex.MultiDex;
import android.text.TextUtils;

import com.amap.api.navi.AMapNavi;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.Utils;
import com.feihong.txgw.model.bean.user.CityInfo;
import com.feihong.txgw.utils.ACache;
import com.feihong.txgw.utils.UserLoginInfo;
import com.feihong.txgw.utils.manage.ThreadPoolManager;
import com.meiqia.meiqiasdk.util.MQConfig;
import com.pddstudio.preferences.encrypted.EncryptedPreferences;
import com.tencent.bugly.Bugly;
import com.tencent.bugly.beta.Beta;
import com.tencent.tinker.entry.DefaultApplicationLike;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.socialize.PlatformConfig;
import com.xiaomi.mipush.sdk.MiPushClient;
import com.xiaomi.mistatistic.sdk.MiStatInterface;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Sabirjan on 2017/6/12.
 * TXGW.
 * 飞宏电子
 */
public class App extends DefaultApplicationLike {

    /**
     * {@link com.feihong.txgw.ui.activity.product.DaoHangActivity 状态栏修改}
     */

    private ACache aCache;
    /**
     * 不需要签名的路径
     */
    public List<String> mSpecialPath;
    /**
     * 小米推送KEY
     */
    private static final String APP_ID = "2882303761517601474";
    private static final String APP_KEY = "5861760115474";
    public static final String GAODE_APP_KET = "c84543a8d93daa84de92a9533c9e5f27";
    private static final String KEY_MEIQIA_KEFU = "84275301caf70aa5c9310cae2686d36d";

    private static final String GAODE_KEY = "dc176838e70e6878ead542ffcdb2ffe8";

    public App(Application application, int tinkerFlags, boolean tinkerLoadVerifyFlag, long applicationStartElapsedTime, long applicationStartMillisTime, Intent tinkerResultIntent) {
        super(application, tinkerFlags, tinkerLoadVerifyFlag, applicationStartElapsedTime, applicationStartMillisTime, tinkerResultIntent);
    }

    public EncryptedPreferences getEncryptedPreferences() {
        return encryptedPreferences;
    }

    private EncryptedPreferences encryptedPreferences = null;
    private static App app = null;

    /**
     * 渠道号
     * 官网
     * 应用宝  华为   小米    360
     * 百度系(百度应用市场  安卓市场  91市场)
     * 阿里系(uc应用商店 pp助手安卓版  淘宝手机助手  豌豆荚)
     */
    private static final String CHANNEL = "官网";

    public static App getApp() {
        return app;
    }

    public long lastUpdateToken = 0;

    @Override
    public void onCreate() {
        super.onCreate();
        /**
         * 设置本包为开发设备包
         */
        Bugly.setIsDevelopmentDevice(getApplication(),true);
        // 初始化Tinker  第二个参数为APPID,第三个参数判断当前是否是Debug模式,签名时改为false
        /**
         * true为测试基准包
         */
        Bugly.init(getApplication(), "1696fd79b2", false);
        app = this;
        aCache = ACache.get(getApplication());
        initUtils();
    }

    public ACache getaCache() {

        return aCache;
    }

    private void initUtils() {


        mSpecialPath = new ArrayList<>();
        mSpecialPath.add("/faceid/v2/verify");
        mSpecialPath.add("/App/Orderphonenum/order");
        mSpecialPath.add("/App/Orderphonenum/yuyue_order");

        initPush();
        initShare();

        Utils.init(getApplication());
        encryptedPreferences = new EncryptedPreferences.Builder(getApplication()).withEncryptionPassword("password").build();

        //小米移动统计
        MiStatInterface.initialize(getApplication(), APP_ID, APP_KEY, CHANNEL);

        //美洽
        MQConfig.init(getApplication(), KEY_MEIQIA_KEFU,null);
        ThreadPoolManager.getInstance().execute(new Runnable() {
            @Override
            public void run() {
                CityInfo cityInfo = UserLoginInfo.getUserCityInfo();
                if (cityInfo == null || TextUtils.isEmpty(cityInfo.getCityCode()) || TextUtils.isEmpty(cityInfo.getCityName()) || TextUtils.isEmpty(cityInfo.getLatitude())) {
                    //设置默认的城市。乌鲁木齐
                    cityInfo = new CityInfo();
                    cityInfo.setCityName("乌鲁木齐市");
                    cityInfo.setCityCode("0991");
                    cityInfo.setLongitude("87.564987");
                    cityInfo.setLatitude("43.840382");
                    UserLoginInfo.setUserCityInfo(cityInfo);
                }
            }
        });
        AMapNavi.setApiKey(getApplication(), GAODE_APP_KET);
        LogUtils.getConfig().setLogSwitch(true);

    }

    /**
     * 初始化分享
     */
    private void initShare() {
        // 第二个参数   appkey  第三个参数  手机型号  第四个参数 友盟推送的appSecret 没有就传空
        UMConfigure.init(getApplication(), "59ba622a3eae25299400004f", "Umeng", UMConfigure.DEVICE_TYPE_PHONE, null);
        //微信 appid appsecret
        PlatformConfig.setWeixin("wx686563a7ca0bcfd5", "36e60979d8036cf8d83e6832ba1aa788");
        // QQ和Qzone appid appkey  通讯100
        PlatformConfig.setQQZone("1106341607", "ZCpzjyk64aUvqJIp");

//        PlatformConfig.setAlipay("2015111700822536");
    }

    /**
     * 初始化推送
     */
    public void initPush() {
        // 注册push服务，注册成功后会向DemoMessageReceiver发送广播
        // 可以从DemoMessageReceiver的onCommandResult方法中MiPushCommandMessage对象参数中获取注册信息
        if (shouldInit()) {
            MiPushClient.registerPush(getApplication(), APP_ID, APP_KEY);
        }
    }

    private boolean shouldInit() {
        ActivityManager am = ((ActivityManager) getApplication().getSystemService(Context.ACTIVITY_SERVICE));
        List<ActivityManager.RunningAppProcessInfo> processInfos = am.getRunningAppProcesses();
        String mainProcessName = getApplication().getPackageName();
        int myPid = android.os.Process.myPid();
        for (ActivityManager.RunningAppProcessInfo info : processInfos) {
            if (info.pid == myPid && mainProcessName.equals(info.processName)) {
                return true;
            }
        }
        return false;
    }
    @Override
    public void onBaseContextAttached(Context base) {
        super.onBaseContextAttached(base);
        MultiDex.install(getApplication());
        // 安装Tinker
        Beta.installTinker(this);
    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public void registerActivityLifecycleCallback(Application.ActivityLifecycleCallbacks callbacks) {
        getApplication().registerActivityLifecycleCallbacks(callbacks);
    }
}


/************************************
 * activity.order ====>订单相关的Activity页面
 * activity.setting ====>设置相关的Activity页面
 * activity.user  ====> 用户相关的Activity页面
 * activity====>其他一些Activity页面
 *
 * adapter ===>各个页面的ListView,GridView,RecyclerView 适配器
 *
 * app === > Application
 *
 * db ===>数据库操作类和Model
 *
 * fragment.home  ===>首页四大页面
 *  fragment === >其他fragment
 *
 * http  === >网络相关的类
 *
 * json ===> GSON 解析类
 *
 * model  ===> JSON对象对应的类
 *
 * utils ===> 其他一些类库
 *
 * view ===> 自定义界面

 ***********************************/

